#include "mq_connection.hpp"
#include <jsoncpp/json/json.h>

int main()
{
    //1.定义异步工作线程
    mq::AsyncWorker::ptr worker = std::make_shared<mq::AsyncWorker>();
    //2.创建连接
    mq::Connection::ptr conn = std::make_shared<mq::Connection>("127.0.0.1",8888,worker);
    //3.创建信道
    mq::Channel::ptr channel = conn->openChannel();
    //4. 通过信道提供的服务完成所需
        //1.创建一个交换机
    google::protobuf::Map<std::string, std::string> args;
    channel->declareExchange("exchange1",mq::ExchangeType::FANOUT,true,false,args);
        //2.创建队列1
    channel->declareQueue("queue1",false,true,false,args);
        //3.创建队列2
    // channel->declareQueue("queue2",false,true,false,args);
        //4.绑定队列
    channel->queueBind("exchange1","queue1","queue1");
    // channel->queueBind("exchange1","queue2","news.music.#");
        //5. 循环向交换机发布消息
    // for(int i=0;i<5;i++)
    // {
    //     mq::BasicProperties bp;
    //     bp.set_id(mq::UUIDHelper::uuid());
    //     bp.set_delivery_mode(mq::DeliveryMode::DURABLE);
    //     bp.set_routing_key("queue1");
    //     channel->basicPublish("exchange1", &bp, "Hello World-" + std::to_string(i));
    // }

    mq::BasicProperties bp;
    bp.set_id(mq::UUIDHelper::uuid());
    bp.set_delivery_mode(mq::DeliveryMode::DURABLE);
    bp.set_routing_key("news.music.sport");
    Json::Value root;
    root["id"] = std::string("123");
    root["name"] = "小明";
    root["number"] = 12; 
    Json::FastWriter writer;
    std::string mess_json = writer.write(root);
    channel->basicPublish("exchange1", &bp,mess_json);

    // bp.set_routing_key("queue1");
    //  channel->basicPublish("exchange1", &bp, "Hello chileme?");

        //6. 关闭信道
    conn->closeChannel(channel);
    return 0;
}